package com.zg.shop.product;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zg.shop.category.Category;
import com.zg.shop.category.CategoryService;
import com.zg.shop.categorysecond.CategorySecond;
import com.zg.shop.categorysecond.CategorySecondService;

public class ProductAction extends ActionSupport implements ModelDriven<Product>{
	private Integer cid;//一级分类的id
	private CategoryService categoryService;
	private Integer page;//页数
	private ProductService productService;
	private PageBean<Product> pageBean;
	private Product product = new Product();
	private Integer csid;
	private CategorySecondService categorySecondService;
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	
	
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}
	public void setCid(Integer cid){
		this.cid = cid;
	}
	public Integer getCid() {
		return cid;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public PageBean<Product> getPageBean() {
		return pageBean;
	}
	public void setCsid(Integer csid) {
		this.csid = csid;
	}
	public Integer getCsid() {
		return csid;
	}
	
	@Override
	public Product getModel() {
		return product;
	}
	
	public String findByCid(){
		//查询所以一级分类
		List<Category> categorys = categoryService.findAll();
		//压入值栈
		ActionContext.getContext().getValueStack().set("categorys", categorys);
		//System.out.println("==========================");
		//for()
		pageBean = productService.findByPage(cid,page);
		return "findByCidSuccess";
	}
	
	public String findProductInfoByPid(){
		List<Category> categorys = categoryService.findAll();
		//压入值栈
		ActionContext.getContext().getValueStack().set("categorys", categorys);
		
		product = productService.findProductInfoByPid(product.getPid());
		return "findProductInfoByPidSuccess";
	}
	
	public String findByCsId(){
		List<Category> categorys = categoryService.findAll();
		//压入值栈
		ActionContext.getContext().getValueStack().set("categorys", categorys);
		
		pageBean = productService.findByCsId(csid, page);
		return "findByCsIdSuccess";
	}
	
	public String adminFindAll(){
		pageBean = productService.findByPage(page);
		return "adminFindAllSuccess";
	}
	public String addPage(){
		List<CategorySecond> categorySeconds = categorySecondService.findAll();
		ActionContext.getContext().getValueStack().set("categorySeconds", categorySeconds);
		return "addPageSuccess";
	}
	public String save() throws IOException{
		String path = ServletActionContext.getServletContext().getRealPath("/products");
		String realPath = path+"/"+csid+"/"+uploadFileName;
		File diskFile = new File(realPath);
		FileUtils.copyFile(upload, diskFile);
		
		CategorySecond categorySecond = new CategorySecond();
		categorySecond.setCsid(csid);
		product.setCategorySecond(categorySecond);
		product.setPdate(new Date());
		product.setImage("products/" +csid + "/"+uploadFileName);
		productService.save(product);
		
		return "saveSuccess";
	}
}
